﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ContactCRUD.UserControls
{
    /// <summary>
    /// Interaction logic for Contact.xaml
    /// </summary>
    public partial class Contact : UserControl
    {
        private int _numberOfErrorsInForm = 0;
        
        public Contact()
        {
            InitializeComponent();
        }
        public Contact(ViewModel.ContactViewModel viewModel):this()
        {
            if (_viewModel == null)
            {
                _viewModel = viewModel;
            }
        }

        private ViewModel.ContactViewModel _viewModel;
        public ViewModel.ContactViewModel ViewModel
        {
            get { return _viewModel; }
            set { _viewModel = value; }
        }
        private void ValidationError(object sender, ValidationErrorEventArgs e)
        {
            
            if (e.Action == ValidationErrorEventAction.Added)
            {
                _numberOfErrorsInForm++;
            }
            else
            {
                _numberOfErrorsInForm--;
            }
        }
        
        private void AddContact_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _numberOfErrorsInForm == 0;
            e.Handled = true;
        }
        #region Public Proprietes
        DependencyProperty companyId = DependencyProperty.Register("CompanyId", typeof(int), typeof(Contact), new PropertyMetadata(0));
        /// <summary>
        /// The error message to display when the validation failes 
        /// </summary>
        public int CompanyId
        {
            get { return (int)GetValue(companyId);}
            set {SetValue(companyId, value);}
        }

        #endregion
    }
}
